﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Source.Object_Classes;
using FrameWork.Source.Animation_Classes;
using FrameWork.Source.Animation_class;
using FrameWork.Source.Resource_Manager;
using Microsoft.Xna.Framework;
using Boomer.Source.TiledMap;

namespace Boomer.Source.Character
{
    class CCharacter : CAnimatedObject
    {
        public enum DIRECTION
        {
            NONE = 0 ,
            RIGHT = 1 ,
            LEFT = 2 ,
            TOP = 3 ,
            BOT = 4
        };

        public DIRECTION currDir { get; private set; }
        protected CTiledMap map;
        public int maxHP { get; private set; }
        public int maxMP { get; private set; }
        public int hp { get; set; }
        public int mp { get; set; }
        public int dame { get; set; }
        public int def { get; set; }
        public int numBoom { get; set; }
        public int numActiveBoom { get; set; }
        public float speed { get; set; }
        public float maxSpeed { get; set; }
        public int boomStr { get; set; }

        public Vector2 mapPos { get; set; }

        public bool isDying { get; set; }

        public CCharacter()
            : base()
        {
            map = null;
            maxHP = 1;
            maxMP = 1;
            hp = 1;
            mp = 1;
            dame = 1;
            def = 0;
            numBoom = 5;
            numActiveBoom = 3;
            speed = 200.0f;
            maxSpeed = 500.0f;
            boomStr = 1;
        }

        public void setDying()
        {
            this.hp -= 1;
            if (this.hp <= 0)
            {
                Effect.CDyingEffect pEff = new Effect.CDyingEffect();
                pEff.init();
                pEff.createEffect( this.getBoundBox() );
                Effect.EffectManager.getInstance().addObject(pEff);
                isDying = true;
                m_isColliable = false;
            }
        }

        public void setPosInMap(int row, int col)
        {
            Vector2 pos = new Vector2(this.map.m_vecMapPos.X + this.map.m_iCellSize * col, this.map.m_vecMapPos.Y + this.map.m_iCellSize * row);
            this.m_vec2Position = pos;

            float xPos = pos.X - (this.getBoundBox().Left - pos.X - 5);
            float yPos = pos.Y - (this.getBoundBox().Top - pos.Y - 5);

            

            this.m_vec2Position = new Vector2(xPos, yPos);

            this.mapPos = new Vector2(row, col);
        }

        public void attachMap(CTiledMap map)
        {
            this.map = new CTiledMap();
            this.map = map;
        }

        public override void update(float deltaTime)
        {
            CTiledMap.updateDeep updateDeep = new CTiledMap.updateDeep(CTiledMap.updateDeepValue);

            this.m_fDeep = updateDeep(map.convertToMapPos(new Vector2(this.getBoundBox().X, this.getBoundBox().Y)), map);
            

            updateAnimation(deltaTime);

            CTiledMap.collisionChecking collision = new CTiledMap.collisionChecking(CTiledMap.collisionMap);
            collision(this, map);

            base.update(deltaTime);
        }

        public override void draw()
        {
            if (m_isVisible)
                m_pSprite.draw(new Vector2(m_vec2Position.X , m_vec2Position.Y - 10), m_fDeep, m_recSource);
        }

        public virtual void move(DIRECTION dir , float speed)
        {
            
            switch (dir)
            {
                case DIRECTION.RIGHT :
                    currDir = DIRECTION.RIGHT;
                    Move(m_vec2Velocity.X + speed, 0);
                    break;
                case DIRECTION.LEFT :
                    currDir = DIRECTION.LEFT;
                    Move(-m_vec2Velocity.X + speed, 0);
                    break;
                case DIRECTION.TOP :
                    currDir = DIRECTION.TOP;
                    Move(0, -m_vec2Velocity.Y + speed);
                    break;
                case DIRECTION.BOT :
                    currDir = DIRECTION.BOT;
                    Move(0, m_vec2Velocity.Y + speed);
                    break;
            }
            
            
        }

        public virtual void processInput(float deltaTime)
        {

        }

        public override bool Collision(ref CObject pObj)
        {
            if (this.getBoundBox().Intersects(pObj.getBoundBox()))
            {
                if (pObj.Tag == FrameWork.Tag_Manager.TagManager.TAGGED.Player)
                {
                    if (this.Tag == FrameWork.Tag_Manager.TagManager.TAGGED.Enemy && this.m_isColliable && pObj.m_isColliable)
                    {
                        ((CPlayer)pObj).setDying();
                        return true;
                    }
                }
            }
            return base.Collision(ref pObj);
        }
    }
}
